<?php
namespace app\admin\common\service;


use think\Db;
use think\facade\Session;

class RbacSrv
{
    /*
     * 获取指定用户的权限
     */
    public function getAdminAccess($uid)
    {
        $access = Db::table('qi_role_admin')->alias('a')
            ->join('qi_role_menu b', 'a.role_id = b.role_id')
            ->where('a.user_id',$uid)
            ->select();

        $menuIdData = [];
        foreach ($access as $item) {
            $menuIdData[] = $item['menu_id'];
        }

        $menuIdData = array_merge(array_unique($menuIdData));

        if (empty($menuIdData)) {
            return [];
        }

        $menuData = Db::table('qi_menu')->where('id','in',$menuIdData)->select();

        $menuLink = [];
        foreach ($menuData as $item) {
            $menuLink[] = $item['link'];
        }

        Session::set('adminAccess',$menuLink);
    }

    /*
     * 验证权限
     */
    public function checkPower($url)
    {
        $hasAccess = Session::get('adminAccess');

        foreach ($hasAccess as $item) {
            if (strpos($url,$item) !== false) {
                return true;
            }
        }

        return false;
    }
}